#include "planedragger.h"
#include "transformable.h"

namespace ControlCenter
{
	PlaneDragger::PlaneDragger():_parentNode(NULL)
	{
		_planeDragger = new osgManipulator::TabPlaneDragger();
		_planeDragger->setupDefaultGeometry();
	}

	PlaneDragger::~PlaneDragger()
	{}

	void PlaneDragger::addDraggerToScene(CoreBase::Transformable* transformable)
	{
		if(transformable!=NULL)
		{
			removeDraggerFromScene();
			_transformAble = transformable;
			osg::MatrixTransform* transform = transformable->getMatrixNode();
			_parentNode = transform->getParent(0);
			//_parentNode->removeChild(transform);
			_parentNode->addChild(_planeDragger);
			float scale = transform->getBound().radius() * 1.0f;
			_planeDragger->setMatrix(osg::Matrix::scale(scale, scale, scale) *
							   osg::Matrix::translate(transform->getBound().center()));
			_planeDragger->addTransformUpdating(transform);
			_planeDragger->setHandleEvents(true);
			_planeDragger->setActivationModKeyMask(osgGA::GUIEventAdapter::MODKEY_CTRL);
			_planeDragger->setActivationKeyEvent('a');
		}
	}

	void PlaneDragger::removeDraggerFromScene()
	{
		if(_parentNode!=NULL)
		{
			_parentNode->removeChild(_planeDragger);
			_transformAble = NULL;
			_parentNode = NULL;
		}
	}
}